package com.xinyu.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "notification.mail")
public class NotificationProperties {

    private boolean enabled;
    private String from = "no-reply@xysemitech.com";
    private String portalBaseUrl = "https://rp.xysemitech.com";
    private String taskDetailPath = "/task?focus=";
    private String dailyDetailPath = "/daily-detail/";

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getPortalBaseUrl() {
        return portalBaseUrl;
    }

    public void setPortalBaseUrl(String portalBaseUrl) {
        this.portalBaseUrl = portalBaseUrl;
    }

    public String getTaskDetailPath() {
        return taskDetailPath;
    }

    public void setTaskDetailPath(String taskDetailPath) {
        this.taskDetailPath = taskDetailPath;
    }

    public String getDailyDetailPath() {
        return dailyDetailPath;
    }

    public void setDailyDetailPath(String dailyDetailPath) {
        this.dailyDetailPath = dailyDetailPath;
    }

    public String buildTaskDetailUrl(Integer taskId) {
        if (taskId == null) {
            return null;
        }
        return sanitizeBaseUrl(portalBaseUrl) + ensurePrefix(taskDetailPath) + taskId;
    }

    public String buildDailyDetailUrl(Integer dailyId) {
        if (dailyId == null) {
            return null;
        }
        return sanitizeBaseUrl(portalBaseUrl) + ensurePrefix(dailyDetailPath) + dailyId;
    }

    private String sanitizeBaseUrl(String baseUrl) {
        if (baseUrl == null) {
            return "";
        }
        return baseUrl.endsWith("/") ? baseUrl.substring(0, baseUrl.length() - 1) : baseUrl;
    }

    private String ensurePrefix(String path) {
        if (path == null || path.isEmpty()) {
            return "/";
        }
        return path.startsWith("/") ? path : "/" + path;
    }
}
